package org.lq.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 获取数据库连接,并且进行连接的关闭操作
 * @author linne
 *
 */
public class JDBCUtil {
	
	
	private static final String DRIVER="com.mysql.jdbc.Driver";
	private static final String URL="jdbc:mysql://localhost:3306/jdbc";
	private static final String USER="root";
	private static final String PASSWORD="root";
	
	//静态代码块,加载驱动,驱动只需要加载一次,所有可以放在静态代码块中
	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 数据库连接工具类
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			 conn = DriverManager.getConnection(URL, USER, PASSWORD);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 关闭所有连接操作
	 * @param rs
	 * @param stem
	 * @param conn
	 */
	public static void closeAll(ResultSet rs,Statement ps,Connection conn) {
		
		try {
			if(rs!=null) {
				rs.close();
				rs= null;
			}
			if(ps != null) {
				ps.close();
			}
			if(conn!=null) {
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	

}






